package com.examination.controller.common_module;

import com.examination.bean.*;
import com.examination.controller.BaseServletController;
import com.examination.service.IExamePaperDetailService;
import com.examination.service.ISelectionAdminService;
import com.examination.service.impl.ExamePaperDetailService;
import com.examination.service.impl.SelectionAdminService;
import com.examination.utils.Constants;
import com.examination.utils.PageInfo;
import com.examination.utils.ResponseResult;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 试题库详细信息模块
 */
@WebServlet(name = "/ExamePaperDetailServlet", urlPatterns = "/ExamePaperDetailServlet")
public class ExamePaperDetailServlet extends BaseServletController {
    private IExamePaperDetailService examePaperDetailService = new ExamePaperDetailService();
    /**
     * 获得试题详细列表
     * @param request
     * @param response
     * @return
     */
    public String getExamePaperDetailList(HttpServletRequest request, HttpServletResponse response){
        //TODO 查询出考试卷详细信息
        ExamePaperDetail examePaperDetail = new ExamePaperDetail();
        examePaperDetail = getObjectByRequest(examePaperDetail,request);
        List<Map> list = examePaperDetailService.getExamePaperDetailList(examePaperDetail);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState("200");
        responseResult.setList(list);
        JSONObject resResultJson = new JSONObject(responseResult);
        rendDate("application/json;charset=utf8", resResultJson.toString(), response);
        return Constants.RESULT_SUCCESS;
    }

    /**
     * 进行考试
     * @param request
     * @param response
     * @return
     */
    public String testExamePaper(HttpServletRequest request, HttpServletResponse response){
        User userInfo = (User)request.getSession().getAttribute("userInfo");//同学id
        if(null != userInfo){
            String examePaperId = request.getParameter("examePaperId");//试卷id
            ExamePaperScore examePaperScore = new ExamePaperScore();
            examePaperScore.setStuId(userInfo.getId());
            examePaperScore.setExamePaperId(Long.parseLong(examePaperId));
            examePaperDetailService.addExamePaperScore(examePaperScore);
            try {
                request.getRequestDispatcher("/examination/common_module/e_selection_menu_left.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  Constants.RESULT_SUCCESS;
    }
    /**
     *
     * @param request
     * @param response
     * @return
     */
    public String submitExamePaper(HttpServletRequest request, HttpServletResponse response){
        //TOOD  学生答题的答案存库并且自动判分
        Map<String,String[]> paramMap = request.getParameterMap();
        User userInfo = (User)request.getSession().getAttribute("userInfo");//同学id
        if(null != userInfo){
            Long userId = userInfo.getId();
        }
        String examePaperId = request.getParameter("examePaperId");//试卷id
        String scheme_type = "single_choice";
        int answer_seq = 1;
        String[] answers = paramMap.get(scheme_type+"_answer"+answer_seq);
        System.out.println(answers);
        return null;
    }

    /**
     * 获得所有的试卷列表
     * @param request
     * @param response
     * @return
     */
    public String getExamePaperList(HttpServletRequest request, HttpServletResponse response){
        ExamePaper examePaper = new ExamePaper();
        List<ExamePaper> list = examePaperDetailService.getExamePaperList(examePaper);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState("200");
        responseResult.setList(list);
        JSONObject resResultJson = new JSONObject(responseResult);
        rendDate("application/json;charset=utf8", resResultJson.toString(), response);
        return Constants.RESULT_SUCCESS;
    }



}
